﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PGMHelper;
using Chart3D;

namespace WSAP
{
    public partial class ModelVisibleDynamic : Form
    {
        /// <summary>
        /// 有限元模型
        /// </summary>
        private WModelNet Model { set; get; }

        /// <summary>
        /// 绘图类型
        /// </summary>
        private StructrueChartParam Param { set; get; }

        /// <summary>
        /// 绘图的最小值及最大值
        /// </summary>
        private ChartStyle chartStyle { set; get; }
         
        /// <summary>
        /// 构造函数
        /// </summary>
        public ModelVisibleDynamic(StructrueChartParam param)
        {
            InitializeComponent();

            #region 绘三维图时启用
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            //When this flag is set to true,
            //the control paints itself and is not painted by the system operation
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //when set Control.DoubleBuffered to true, 
            //it will set the ControlStyles.AllPaintingInWmPaint and ControlStyles.OptimizedDoubleBuffer to true    
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            #endregion

            //获得参数
            this.Param = param;
            //是否存在动态绘图对象
            if (param.DynamicDrawingList == null)
            {
                MessageBoxExtern.Error("需定义StructrueChart.DynamicDrawingList！");
                return;
            }
            //修改限值
            this.trkTime.Maximum = param.DynamicDrawingList.Count - 1;
            //修改Label
            this.TimeMaxLabel.Text = this.trkTime.Maximum.ToString();
            //this.trkAzimuth.sc
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="model"></param>
        public ModelVisibleDynamic(WModelNet model, StructrueChartParam param) : this(param)
        {
            this.Model = model;
            this.RefreshBackground();
        }

        /// <summary>
        /// 重新绘图，更新图片
        /// </summary>
        private void RefreshBackground()
        {
            //获得三维绘图
            var chart3D = this.Model.GetChart3D(this.PlotPanel, this.Param, this.trkTime.Value);
            //是否存在边界
            if (this.chartStyle == null) this.chartStyle = chart3D.ChartStyle;
            else chart3D.ChartStyle.SetChartBound(this.chartStyle);
            //绘图
            chart3D.ToGraphics(this.trkElevation.Value, this.trkAzimuth.Value, isAixsToG: false);
        }

        /// <summary>
        /// 修改视高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trkElevation_Scroll(object sender, EventArgs e)
        {
            tbElevation.Text = trkElevation.Value.ToString();
            this.RefreshBackground();
        }

        /// <summary>
        /// 修改视角
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trkAzimuth_Scroll(object sender, EventArgs e)
        {
            tbAzimuth.Text = trkAzimuth.Value.ToString();
            this.RefreshBackground();
        }

        /// <summary>
        /// 修改波放时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trkTime_Scroll(object sender, EventArgs e)
        {
            this.TimeMaxLabel.Text = this.trkTime.Value.ToString();
            this.RefreshBackground();
        }

        /// <summary>
        /// 大小更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlotPanel_Resize(object sender, EventArgs e)
        {
            this.RefreshBackground();
        }
    }
}
